package model;

import java.awt.Dimension;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Resuable Table class used for storing data
 * @author Bruce, David, Rex
 */
public class Table {

    // Store the data from the input data file. All the data are in String
    // format.
    @SuppressWarnings("rawtypes")
    private List<ArrayList<Comparable>> myTable = new ArrayList<ArrayList<Comparable>>();
    // Store the field data type of the input data file ("Label" or "Number").
    private List<String> myFieldTypes = new ArrayList<String>();

    /**
     * construct a Table
     * @param table data list
     * @param types data type of each column
     */
    @SuppressWarnings("rawtypes")
    public Table (List<ArrayList<Comparable>> table, List<String> types) {
        myTable = table;
        myFieldTypes = types;
    }

    /**
     * Get data of a cell.
     * 
     * @param rowIndex row index
     * @param columnIndex column index
     * @return Data object
     */
    public Data getData (int rowIndex, int columnIndex) {
        return new Data(myTable.get(rowIndex).get(columnIndex),
                myFieldTypes.get(columnIndex));
    }

    /**
     * Get data types in data fields
     * 
     * @return
     */
    public List<String> getFieldTypes () {
        return myFieldTypes;
    }

    /**
     * Get table dimension
     * 
     * @return
     */
    public Dimension getDimension () {
        Dimension myTableDimension = new Dimension(myFieldTypes.size(),
                myTable.size());
        return myTableDimension;
    }

    /**
     * Sort the table according to the indicated column in ascending order
     * 
     * @param columnIndex index of the column which determines the order
     */
    public void sortAscending (int columnIndex) {
        Collections.sort(myTable,
                        new TableComparator(columnIndex, myFieldTypes.get(columnIndex)));
    }

    /**
     * Sort the table according to the indicated column in descending order
     * 
     * @param columnIndex index of the column which determines the order
     */
    public void sortDescending (int columnIndex) {
        Collections.sort(myTable, Collections.reverseOrder(new TableComparator(
                columnIndex, myFieldTypes.get(columnIndex))));

    }

    /**
     * Get the number of rows (ie. number of data sets)
     * @return size
     */
    public int getDataSize () {
        return myTable.size();
    }

    /**
     * Get the row index of the max value within a single column.
     *
     * @param columnIndex index of the column which determines the order
     * @return
     */
    public int maxData (int columnIndex) {
        double maxValue = Double.parseDouble(myTable.get(0).get(columnIndex).toString());
        int maxValueRowIndex = 0;
        for (int i = 0; i < myTable.size(); i++) {
            if (Double.parseDouble(myTable.get(i).get(columnIndex).toString()) > maxValue) {
                maxValue = Double.parseDouble(myTable.get(i).get(columnIndex).toString());
                maxValueRowIndex = i;
            }
        }

        return maxValueRowIndex;
    }

    /**
     * Get the row index of the min value within a single column.
     *
     * @param columnIndex index of the column which determines the order
     * @return
     */
    public int minData (int columnIndex) {
        double minValue = Double.parseDouble(myTable.get(0).get(columnIndex).toString());
        int minValueRowIndex = 0;
        for (int i = 0; i < myTable.size(); i++) {
            if (Double.parseDouble(myTable.get(i).get(columnIndex).toString()) < minValue) {
                minValue = Double.parseDouble(myTable.get(i).get(columnIndex).toString());
                minValueRowIndex = i;
            }
        }
        return minValueRowIndex;
    }
}
